""""
题目描述
给出每个节点的两个儿子节点，建立一棵二叉树（根节点为 1），
如果是叶子节点，则输入0 0。建好树后希望知道这棵二叉树的深度。
二叉树的深度是指从根节点到叶子结点时，最多经过了几层。

最多有 10^6结点。

输入输出样例
输入 #1复制
7
2 7
3 6
4 5
0 0
0 0
0 0
0 0

输出 #1复制
4
https://www.luogu.com.cn/problem/P4913

"""
import collections

from mode import PrintTree


class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None


def generateTree(n, treeList):
    hashMap = {}
    for i in range(1, n + 1):
        node = TreeNode(i)
        hashMap[i] = node
    for j in range(1, n + 1):
        nodeFather = hashMap[j]
        nodeFather.left = hashMap.get(int(treeList[j - 1][0]), None)
        nodeFather.right = hashMap.get(int(treeList[j - 1][1]), None)
    return hashMap[1]


while True:
    try:
        n = int(input())
        treeList = []
        for i in range(n):
            treeList.append(input().split())
        root = generateTree(n, treeList)

        deque = collections.deque()
        deque.append(root)
        res = 0
        while deque:
            for i in range(len(deque)):
                node = deque.popleft()
                if node.left:
                    deque.append(node.left)
                if node.right:
                    deque.append(node.right)
            res += 1
        print(res)
        break
    except:
        break
"""
7
2 7
3 6
4 5
0 0
0 0
0 0
0 0

"""
